import api from "@/api/apiServer";
let sourceData;
async function getData(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  sourceData = res.data;
}
getData("dict.source");

let SupplierType;
async function getTypeList() {
  const res = await api.SupplierTypeList();
  SupplierType = res.data;
}
getTypeList();
// 格式化数据来源
function caseSource(value, option) {
  let result = option.filter((item) => {
    return item.id === value;
  });

  if (result.length > 0) {
    return result[0].dictName;
  } else {
    return "";
  }
}
//供应商类型
function caseSupplierType(value, option) {
  for (let i in option) {
    if (option[i].id == value) {
      return [option[i].name];
    }
    if (option[i].children) {
      let node = caseSupplierType(value, option[i].children);
      if (node !== undefined) {
        return node.concat(option[i]);
      }
    }
  }
}

let supplierOption = [];
let enterpriOption = [];
let supplierlevelOption = [];
async function getSupplierOption(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  supplierOption = res.data;
}
async function getEnterpriOption(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  enterpriOption = res.data;
}
async function getSupplierlevelOption(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  supplierlevelOption = res.data;
}
getSupplierOption("dict.supplierStatus", supplierOption);
getEnterpriOption("dict.enterprisenature", enterpriOption);
getSupplierlevelOption("dict.supplierlevel", supplierlevelOption);

const columns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "供应商名称",
    dataIndex: "name",
    align: "center",
    key: "name",
  },
  {
    title: "供应商编号",
    dataIndex: "serialNumber",
    align: "center",
    key: "serialNumber",
  },
  {
    title: "业务员",
    align: "center",
    key: "saleman_name",
    dataIndex: "saleman_name",
  },
  {
    title: "供应商状态",
    align: "center",
    key: "supplierStatus",
    dataIndex: "supplierStatus",
    customRender: (text, record, index) => {
      return caseSource(text, supplierOption);
    },
  },
  {
    title: "供应商类型",
    align: "center",
    key: "suplierTypeId",
    dataIndex: "suplierTypeId",
    customRender: (text, record, index) => {
      return caseSupplierType(text, SupplierType);
    },
  },
  {
    title: "企业性质",
    align: "center",
    key: "enterpriseNature",
    dataIndex: "enterpriseNature",
    customRender: (text, record, index) => {
      return caseSource(text, enterpriOption);
    },
  },
  {
    title: "供应商等级",
    align: "center",
    key: "supplierLevel",
    dataIndex: "supplierLevel",
    customRender: (text, record, index) => {
      return caseSource(text, supplierlevelOption);
    },
  },
  {
    title: "品牌",
    align: "brand",
    dataIndex: "brand",
    key: "contact",
  },
  {
    title: "联系电话",
    align: "center",
    key: "contactPhone",
    dataIndex: "contactPhone",
  },
  {
    title: "操作",
    width: 150,
    dataIndex: "action",
    align: "center",
    scopedSlots: { customRender: "action" },
  },
];

const user = [
  {
    title: "序号",
    width: 80,
    dataIndex: "index",
    key: "index",
    align: "center",
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    title: "员工姓名",
    dataIndex: "name",
    align: "center",
    key: "name",
  },
  {
    title: "性别",
    align: "center",
    dataIndex: "sex",
    key: "sex",
  },
  {
    title: "状态",
    align: "center",
    key: "staus",
    dataIndex: "staus",
  },
  {
    title: "部门",
    align: "center",
    key: "department",
    dataIndex: "department",
  },
  {
    title: "职务",
    align: "center",
    key: "sortq",
    dataIndex: "sortq",
  },
  {
    title: "身份证号",
    align: "center",
    key: "identityCard",
    dataIndex: "identityCard",
  },
  {
    title: "移动电话",
    align: "center",
    key: "phone",
    dataIndex: "phone",
  },
  {
    title: "籍贯",
    align: "center",
    key: "nativePlace",
    dataIndex: "nativePlace",
  },
  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    fixed: "right",
    width: 150,
    scopedSlots: { customRender: "action" },
  },
];

const suplierColumns = [
  {
    title: "供应商名称",
    dataIndex: "name",
    align: "center",
    key: "name",
  },
  {
    title: "供应商编号",
    dataIndex: "serialNumber",
    align: "center",
    key: "serialNumber",
  },
  {
    title: "业务员",
    align: "center",
    key: "saleman_name",
    dataIndex: "saleman_name",
  },
  {
    title: "供应商状态",
    align: "center",
    key: "supplierStatus",
    dataIndex: "supplierStatus",
    customRender: (text, record, index) => {
      return caseSource(text, supplierOption);
    },
  },
  {
    title: "供应商类型",
    align: "center",
    key: "suplierTypeId",
    dataIndex: "suplierTypeId",
    customRender: (text, record, index) => {
      return caseSupplierType(text, SupplierType);
    },
  },
  {
    title: "企业性质",
    align: "center",
    key: "enterpriseNature",
    dataIndex: "enterpriseNature",
    customRender: (text, record, index) => {
      return caseSource(text, enterpriOption);
    },
  },
  {
    title: "供应商等级",
    align: "center",
    key: "supplierLevel",
    dataIndex: "supplierLevel",
    customRender: (text, record, index) => {
      return caseSource(text, supplierlevelOption);
    },
  },
  {
    title: "品牌",
    align: "brand",
    dataIndex: "brand",
    key: "contact",
  },
  {
    title: "联系电话",
    align: "center",
    key: "contactPhone",
    dataIndex: "contactPhone",
  },
];
export { columns, user, suplierColumns };
